<?php

namespace app\model;

use app\utils\Md5Service;
use \think\facade\Db;
use think\facade\Log;

class PermissionModel
{

    /**
     * 获取所有的权限菜单列表
     * @return void
     */
    public static function GetAllPermission(): array
    {
        return Db::table('xl_partner_permission')->where('status', '=', 1)
            ->select()->toArray();
    }


    /**
     * 根据查询条件获取所有的权限菜单列表
     * @return void
     */
    public static function GetAllPermissionWhere($where): array
    {
        return Db::table('xl_partner_permission')->where('status', '=', 1)->where($where)
            ->select()->toArray();
    }

    /**
     * 根据手机号密码获取登录信息是否正确
     * @return void
     */
    public static function GetAllRole(): array
    {
        return Db::table('xl_partner_role')->where('status', '=', 1)
            ->select()->toArray();
    }

    public static function UpdatePartnerLoginInfo($data, $param)
    {
        $update['login_ip'] = $param['login_ip'];
        $update['login_num'] = $data['login_num'] + 1;
        $update['login_time'] = getNowDateTime();
        Db::startTrans();
        try {
            Db::table('xl_partner_user')->where(['id' => $data['id']])->update($update);
            // 提交事务
            Db::commit();
            return true;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return false;
        }
    }


    /**
     * 获取单个用户所有的权限菜单列表
     * @return void
     */
    public static function GetPermission($role_id): array
    {
        return Db::table('xl_partner_permission')->where('status', '=', 1)
            ->where('id', '=', $role_id)->field('id,permission_ids')
            ->select()->toArray();
    }


    /**
     * 根据查询权限是否存在
     * @return void
     */
    public static function getPermissionInfo($where)
    {
        return Db::table('xl_partner_permission')->where($where)
            ->find();
    }

    //添加合作商新权限

    public static function add_permission($data)
    {
        return Db::table('xl_partner_permission')->insertGetId($data);
    }

    //更新合作商新权限

    public static function modify_permission($data, $permission_id)
    {
        return Db::table('xl_partner_permission')->where('id', '=', $permission_id)->update($data);
    }

    //删除合作商新权限

    public static function del_permission($data, $permission_id)
    {
        return Db::table('xl_partner_permission')->where('id', '=', $permission_id)->delete();
    }
}
